package ${package}.ws.demo;

import javax.jws.WebService;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Required;
import ${package}.entity.demo.User;
import ${package}.service.demo.UserManager;
import ${package}.ws.demo.UserWebService;
import ${package}.ws.Constants;
import ${package}.ws.demo.dto.UserDTO;

import org.springside.modules.webservice.FaultException;
import org.springside.modules.webservice.WebServiceSupport;

/**
 * WebService实现类.
 * 
 * 使用@WebService指向Interface定义类即可.
 * 
 * @author sky
 * @author calvin
 */
@WebService(endpointInterface = "${package}.ws.UserWebService",targetNamespace = Constants.WS_NAMESPACE)
public class UserWebServiceImpl extends WebServiceSupport implements UserWebService {
	
	private UserManager userManager;

	public UserDTO getUser(Integer userId) throws FaultException {

		// 获取User
		User userEnity = userManager.getUser(userId);
		if (userEnity == null) {
			logger.error("帐号ID：{}不存在",userId);
			throw new FaultException(ErrorCode.FETCH_DATA_ERROR, "帐号ID" + userId + "不存在");
		}
		// 转换User
		try {
			return (UserDTO) dozer.map(userEnity, UserDTO.class);
		} catch (Exception e) {
			logger.error("dozer转换错误",e);
			throw new FaultException(ErrorCode.DATA_TRANSLATE_ERROR, "数据转换错误");
		}
	}

	public Integer createUser(UserDTO user) throws FaultException {
		
		// 转换User
		User userEntity = null;
		try {
			userEntity = (User) dozer.map(user, User.class);
		} catch (Exception e) {
			logger.error("dozer转换错误",e);
			throw new FaultException(ErrorCode.DATA_TRANSLATE_ERROR, "数据转换错误");
		}
		// 保存User
		try {
			userManager.createUser(userEntity);
			return userEntity.getId();
		} catch (Exception e) {
			logger.error("保存帐号失败", e);
			throw new FaultException(ErrorCode.SAVE_DATA_ERROR, "帐号保存失败");
		}
	}

	@Required
	public void setUserManager(UserManager userManager) {
		this.userManager = userManager;
	}
}